var express = require('express');
var router = express.Router();
let { Shangtypemodel, Peimodel, Dianpumodel, Typemodel, Foodmodel } = require('../db/model.js')
/* GET home page. */
router.post('/addshangtype', function (req, res, next) {
	Shangtypemodel.create(req.body).then(rep => {
		res.send({
			code: 200,
			msg: '数据添加成功'
		})
	})
});
router.post('/addpei', function (req, res, next) {
	Peimodel.create(req.body).then(rep => {
		res.send({
			code: 200,
			msg: '数据添加成功'
		})
	})
});
router.post('/adddianpu', function (req, res, next) {
	Dianpumodel.create(req.body).then(rep => {
		res.send({
			code: 200,
			msg: '数据添加成功'
		})
	})
});
router.post('/addtype', function (req, res, next) {
	Typemodel.create(req.body).then(rep => {
		res.send({
			code: 200,
			msg: '数据添加成功'
		})
	})
});
router.post('/addfood', function (req, res, next) {
	Foodmodel.create(req.body).then(rep => {
		res.send({
			code: 200,
			msg: '数据添加成功'
		})
	})
});
router.get('/shangtype', function (req, res, next) {
	Shangtypemodel.find().then(rep => {
		res.send({
			code: 200,
			data:rep
		})
	})
});
router.get('/pei', function (req, res, next) {
	Peimodel.find().then(rep => {
		res.send({
			code: 200,
			data:rep
		})
	})
});
router.get('/dianpu', function (req, res, next) {
	let {s_km,s_shangtype,s_pei}=req.query
	let search={}
	if(s_shangtype){
		search.cai_type=s_shangtype
	}
	if(s_pei){
		search.pei_type=s_pei
	}
	Dianpumodel.find(search).populate('cai_type').populate('pei_type').sort({Km:Number(s_km)}).then(rep => {
		res.send({
			code: 200,
			data:rep
		})
	})
});
router.get('/type', function (req, res, next) {
	Typemodel.find().then(rep => {
		res.send({
			code: 200,
			data:rep
		})
	})
});
router.get('/food',function (req, res, next){
	let {dian_id,cai_type}=req.query
	let search={}
	if(dian_id){
		search.dian=dian_id
	}
	if(cai_type){
		search.type=cai_type
	}
	Foodmodel.find(search).populate('dian').populate('type').then(rep => {
		res.send({
			code: 200,
			data:rep
		})
	})
});
module.exports = router;
